package com.ljfiction.book.activitys;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.ChapterInforBean;
import com.ljfiction.book.Baner.CreadChapterBookBean;
import com.ljfiction.book.Baner.CreatBeijingBean;
import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.Dialog.TocunDialog;
import com.ljfiction.book.MyView.ContainsEmojiEditText;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.SystemToolUtils;
import com.ljfiction.book.tools.AppUtils;
import com.ljfiction.book.tools.CreatChapterSettingManager;
import com.ljfiction.book.tools.ScreenUtils;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.SoftKeyBoardListener;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class MyBookChapterBianjiActivity extends BaseActivity {

    @BindView(R.id.back_bg_image_chapter)
    ImageView backBgImage;
    @BindView(R.id.volume0k_chapter)
    TextView volume0kChapter;
    @BindView(R.id.volume_article_chapter)
    LinearLayout volumeArticleChapter;
    @BindView(R.id.view1_chapter)
    View view1Chapter;
    @BindView(R.id.zhangjiename_chapter)
    ContainsEmojiEditText zhangjienameChapter;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.zhangjietext_chapter)
    ContainsEmojiEditText zhangjietextChapter;
    @BindView(R.id.scroolrelative_chapter)
    RelativeLayout scroolrelativeChapter;
    @BindView(R.id.scrollview_arti_chapter)
    ScrollView scrollviewArtiChapter;
    @BindView(R.id.back_article_chapter)
    ImageView backArticleChapter;
    @BindView(R.id.textnum_article_chapter)
    TextView textnumArticleChapter;
    @BindView(R.id.yijianpaiban_chapter)
    ImageView yijianpaibanChapter;
    @BindView(R.id.toleft_chapter)
    ImageView toleftChapter;
    @BindView(R.id.toright_chapter)
    ImageView torightChapter;
    @BindView(R.id.set_creat_chapter)
    ImageView setCreatChapter;
    @BindView(R.id.ok_article_chapter)
    TextView okArticleChapter;
    @BindView(R.id.creat_top_chapter)
    LinearLayout creatTopChapter;
    @BindView(R.id.douhaorecycle_chapter)
    RecyclerView douhaorecycleChapter;
    @BindView(R.id.douhaoimage_chapter)
    ImageView douhaoimageChapter;
    @BindView(R.id.view4_chapter)
    View view4Chapter;
    @BindView(R.id.douhaolayout_chapter)
    RelativeLayout douhaolayoutChapter;

    @BindView(R.id.view3_chapter)
    View view3Chapter;

    @BindView(R.id.auhorSay_chapter_addimage)
    ImageView auhorSayChapterAddimage;
    @BindView(R.id.auhorSay_chapter_text)
    TextView auhorSayChapterText;
    @BindView(R.id.authersay_creat_chapter)
    CardView authersayCreatChapter;


    private String bookId;
    private String bookName;
    private String volumId = "1718", volumName = "请选择";
    private Intent intent;//选择卷
    private NoBackgLodDialog noBackgLodDialog;
    private PopupWindow popupWindowset;
    private ViewHolder viewHolder;
    private CreatBeijingBean beijingBean;
    private int beijingposition = 0;//背景所属坐标
    private int isNeight = 0;//是否黑夜，0是白天
    private int seekposition;//亮度
    private int zitiposition;//字大小
    private List<String> douhaoList = new ArrayList<>();
    private boolean zhangjianameisfocus = false, zhangjietextisfocus = false;
    private ArrayList<String> textList = new ArrayList<>();//保存上下内容
    private int textPosition = -1;//显示的内容下表
    private boolean isnochange = false;//是否编写改变的内容
    private ChapterInforBean chapterInforBean;
    private float touchY;
    private String authorSay = "作者什么都没说";
    private TocunDialog tocunDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_book_chapter_bianji);
        ButterKnife.bind(this);
        AppUtils.init(this);
        beijingBean = new CreatBeijingBean();
        beijingposition = CreatChapterSettingManager.getInstance().getCreatBeijingcolor();
        isNeight = CreatChapterSettingManager.getInstance().getIsCreatNightDay();
        setBeijingAll();
        volumId = CreatChapterSettingManager.getInstance().getBookvolumId(bookId) + "";
        seekposition = CreatChapterSettingManager.getInstance().getCreatSeekposition() == 0 ? ScreenUtils.getScreenBrightness() : CreatChapterSettingManager.getInstance().getCreatSeekposition();
        zitiposition = CreatChapterSettingManager.getInstance().getCreatZitiSeekposition();
        zhangjietextChapter.setTextSize(12 + zitiposition);
        bookId = getIntent().getStringExtra("bookId");
        volumName = getIntent().getStringExtra("volumeName");
        volume0kChapter.setText(volumName);
        volumId = getIntent().getStringExtra("volumeId");
        intChapterData();
        intent = new Intent(this, SelectVolumActivity.class);
        intEnvent();
    }

    private void intChapterData() {
        OkHttpUtils.post(HttpManager.IP + "/chapter-getChapterInfo")

                .params("chapterid", getIntent().getStringExtra("chapterId"))
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (noBackgLodDialog == null) {
                            noBackgLodDialog = new NoBackgLodDialog(MyBookChapterBianjiActivity.this, true);
                        }
                        noBackgLodDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (noBackgLodDialog != null) {
                            noBackgLodDialog.dismis();
                        }
                        Gson gson = new Gson();
                        chapterInforBean = gson.fromJson(s, ChapterInforBean.class);
                        if (chapterInforBean.getReturnCode() == 200) {
                            zhangjienameChapter.setText(chapterInforBean.getData().getChapterTitle());
                            zhangjietextChapter.setText(StringTool.getChapterContext(chapterInforBean.getData().getChapterContent()));
                            authorSay = chapterInforBean.getData().getAuthorWords();

                        } else {
                            showToast(chapterInforBean.getMsg());
                            goBack();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        if (noBackgLodDialog != null) {
                            noBackgLodDialog.dismis();
                        }
                        goBack();
                    }
                });
    }


    @SuppressLint("ClickableViewAccessibility")
    private void intEnvent() {
        zhangjietextChapter.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus && chapterInforBean.getData().getChapterIsvip() == 1) {
                    showToast("VIP章节不能修改哦！");
                    return;
                }
                zhangjietextisfocus = hasFocus;
                toleftorright();
            }
        });
        zhangjienameChapter.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                zhangjianameisfocus = hasFocus;

            }
        });
        douhaoList.add("，");
        douhaoList.add("。");
        douhaoList.add("！");
        douhaoList.add("？");
        douhaoList.add("：");
        douhaoList.add("“”");
        douhaoList.add("、");
        douhaoList.add("；");
        BaseQuickAdapter adapter;
        douhaorecycleChapter.setAdapter(adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.douhao_item, douhaoList) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.douhao_text, item);
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (zhangjietextisfocus) {
                    zhangjietextChapter.getText().insert(zhangjietextChapter.getSelectionStart(), douhaoList.get(position));
                    if (position == 5) {
                        zhangjietextChapter.setSelection(zhangjietextChapter.getSelectionStart() - 1);
                    }
                }
                if (zhangjianameisfocus) {
                    zhangjienameChapter.getText().insert(zhangjienameChapter.getSelectionStart(), douhaoList.get(position));
                    if (position == 5) {
                        zhangjienameChapter.setSelection(zhangjienameChapter.getSelectionStart() - 1);
                    }
                }
            }
        });
        douhaorecycleChapter.setLayoutManager(new GridLayoutManager(this, 8));
        douhaoimageChapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SystemToolUtils.hideKeyBoard(MyBookChapterBianjiActivity.this);
            }
        });
        zhangjietextChapter.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                touchY = event.getY();
                return false;
            }
        });
        //监听软键盘
        SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                douhaolayoutChapter.setVisibility(View.VISIBLE);
                authersayCreatChapter.setVisibility(View.GONE);
                view3Chapter.setVisibility(View.GONE);
                Rect r = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                RelativeLayout.LayoutParams olp = (RelativeLayout.LayoutParams) douhaolayoutChapter.getLayoutParams();
                RelativeLayout.LayoutParams scrool = (RelativeLayout.LayoutParams) scrollviewArtiChapter.getLayoutParams();
                olp.setMargins(0, r.bottom - douhaolayoutChapter.getHeight(), 0, r.bottom);
                douhaolayoutChapter.setLayoutParams(olp);
                scrool.height = r.bottom - douhaolayoutChapter.getHeight() - ScreenUtils.getNum(MyBookChapterBianjiActivity.this, 60 + zitiposition);
                scrollviewArtiChapter.setLayoutParams(scrool);
                scrollviewArtiChapter.scrollTo(0, (int) touchY);
                Log.v("MyBooktop", "height" + height + "r.bottom" + r.bottom);
                toleftorright();
            }

            @Override
            public void keyBoardHide(int height) {
                douhaolayoutChapter.setVisibility(View.GONE);
                view3Chapter.setVisibility(View.VISIBLE);
                authersayCreatChapter.setVisibility(View.VISIBLE);
                setCreatChapter.setVisibility(View.VISIBLE);
                toleftChapter.setVisibility(View.GONE);
                torightChapter.setVisibility(View.GONE);
                Rect r = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                RelativeLayout.LayoutParams scrool = (RelativeLayout.LayoutParams) scrollviewArtiChapter.getLayoutParams();
                scrool.height = r.bottom + height - authersayCreatChapter.getHeight();
                Log.v("MyBooktom", "height" + height + "r.bottom" + r.bottom);
                scrollviewArtiChapter.setLayoutParams(scrool);

            }
        });

        zhangjietextChapter.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                //s:变化后的所有字符
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //s:变化前的所有字符； start:字符开始的位置； count:变化前的总字节数；after:变化后的字节数
                if (String.valueOf(s).length() >= 20000) {
                    showToast("字数不能大于20000哦");
                }
            }

            @SuppressLint("SetTextI18n")
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //S：变化后的所有字符；start：字符起始的位置；before: 变化之前的总字节数；count:变化后的字节数


                textnumArticleChapter.setText(StringTool.getChapterTextLength(StringTool.getChapterText(zhangjietextChapter.getText().toString())) + "字");

                if (!isnochange) {
                    if (s.toString().endsWith("\n")) {
                        zhangjietextChapter.setText(zhangjietextChapter.getText() + "        ");
                        zhangjietextChapter.setSelection(zhangjietextChapter.getText().length());
                    }
                    if (textPosition + 1 < textList.size()) {
                        for (int i = textPosition + 1; i < textList.size(); i++) {
                            textList.remove(i);
                        }
                    }
                    if (textList.size() < 10) {
                        textList.add(s.toString());
                        textPosition = textList.size() - 1;
                    } else {
                        textList.add(s.toString());
                        textList.remove(0);
                    }
                }
                isnochange = false;
                toleftorright();

            }
        });
    }

    //撤销和前进
    private void toleftorright() {
        if (zhangjietextisfocus) {
            setCreatChapter.setVisibility(View.GONE);
            toleftChapter.setVisibility(View.VISIBLE);
            torightChapter.setVisibility(View.VISIBLE);
            if (textList.size() > textPosition + 1) {
                torightChapter.setImageResource(beijingBean.getTorightlist().get(beijingposition));
            } else {
                torightChapter.setImageResource(beijingBean.getNotorightlist().get(beijingposition));
            }
            if (textPosition > 0) {
                toleftChapter.setImageResource(beijingBean.getToleftlist().get(beijingposition));
            } else {
                toleftChapter.setImageResource(beijingBean.getNotoleftlist().get(beijingposition));
            }
        } else {
            toleftChapter.setVisibility(View.GONE);
            torightChapter.setVisibility(View.GONE);
            setCreatChapter.setVisibility(View.VISIBLE);
        }

    }


    @OnClick({R.id.volume_article_chapter, R.id.back_article_chapter, R.id.yijianpaiban_chapter, R.id.toleft_chapter, R.id.toright_chapter, R.id.set_creat_chapter, R.id.ok_article_chapter, R.id.authorSay_chapter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.volume_article_chapter:
                intent.putExtra("bookId", bookId);
                intent.putExtra("bookName", bookName);
                startActivityForResult(intent, 1);
                break;

            case R.id.back_article_chapter:
                if (tocunDialog == null) {
                    tocunDialog = new TocunDialog(this);
                    tocunDialog.setListener(new TocunDialog.Listener() {
                        @Override
                        public void back() {
                            goBack();
                        }

                        @Override
                        public void cuntocungao() {
                            tofabu();
                        }
                    }, "尚未发布修改内容，是否发布后退出？", "直接退出", "发布并退出");
                }
                tocunDialog.show();

                break;

            case R.id.yijianpaiban_chapter:
                zhangjietextChapter.setText(StringTool.getPaibanChapter(zhangjietextChapter.getText().toString()));
                showToast("一键排版成功");
                zhangjietextChapter.setSelection(zhangjietextChapter.getText().length());
                break;
            case R.id.toleft_chapter:
                if (textPosition > 0) {
                    isnochange = true;
                    textPosition = textPosition - 1;
                    zhangjietextChapter.setText(textList.get(textPosition));
                    zhangjietextChapter.setSelection(zhangjietextChapter.getText().length());
                    toleftorright();
                }
                break;
            case R.id.toright_chapter:
                if (textList.size() > textPosition + 1) {
                    isnochange = true;
                    textPosition++;
                    zhangjietextChapter.setText(textList.get(textPosition));
                    zhangjietextChapter.setSelection(zhangjietextChapter.getText().length());
                    toleftorright();
                }
                break;
            case R.id.set_creat_chapter:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),
                            0);
                }
                if (popupWindowset != null && !popupWindowset.isShowing()) {
                    popupWindowset.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                } else {
                    showPopuWindow(view);
                }
                break;
            case R.id.ok_article_chapter:
                if (chapterInforBean.getData().getChapterIsvip() == 1) {
                    showToast("VIP章节不能修改哦！");
                    return;
                }
                tofabu();
                break;

            case R.id.authorSay_chapter:
                Intent intent = new Intent(MyBookChapterBianjiActivity.this, AuthorSayActivity.class);
                intent.putExtra("authorsay", authorSay);
                startActivityForResult(intent, 10);
                break;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (tocunDialog == null) {
                tocunDialog = new TocunDialog(this);
                tocunDialog.setListener(new TocunDialog.Listener() {
                    @Override
                    public void back() {
                        goBack();
                    }

                    @Override
                    public void cuntocungao() {
                        tofabu();
                    }
                }, "尚未发布修改内容，是否发布后退出？", "直接退出", "发布并退出");
            }
            tocunDialog.show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    //发布
    private void tofabu() {

        if (volume0kChapter.getText().toString().equals("")) {
            showToast("请选择或创建卷");
        } else if (zhangjienameChapter.getText().toString().equals("")) {
            showToast("章节名不能为空");
        } else if (StringTool.isEmpty(zhangjietextChapter.getText().toString())) {
            showToast("章节内容不能为空");
        } else if (zhangjietextChapter.getText().toString().length() < 10) {
            showToast("章节内容不能少于十个字哦！");
        } else {
            String chapterName = "";
            if (zhangjienameChapter.getText().toString().equals("")) {
                chapterName = "无章节名称";
            } else {
                chapterName = zhangjienameChapter.getText().toString();
            }
            CreadChapterBookBean creadChapterBookBean = new CreadChapterBookBean();
            creadChapterBookBean.setId(String.valueOf(chapterInforBean.getData().getId()));
            creadChapterBookBean.setBookisvip(chapterInforBean.getData().getChapterIsvip());
            creadChapterBookBean.setBookId(bookId);
            creadChapterBookBean.setBookTitle(bookName);
            creadChapterBookBean.setVolumeId(volumId);
            creadChapterBookBean.setChapterTitle(chapterName);
            creadChapterBookBean.setChapterContent(zhangjietextChapter.getText().toString());
            creadChapterBookBean.setAuthorWords(StringTool.getAuthorsayText(authorSay));
            creadChapterBookBean.setIsCaogao(0);
            creadChapterBookBean.setReleastime(chapterInforBean.getData().getReleaseTime());
            creadChapterBookBean.setWhereCome(1);
            Intent intent1 = new Intent(MyBookChapterBianjiActivity.this, CreadChapterOkActivity.class);
            intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent1.putExtra("bookBean", creadChapterBookBean);
            startActivityForResult(intent1, 2);


        }


    }


    private BaseQuickAdapter adapterSet;

    private void showPopuWindow(View view) {
        View contentView = LayoutInflater.from(this).inflate(R.layout.creatset, null);
        popupWindowset = new PopupWindow(contentView);
        viewHolder = new ViewHolder(contentView);
        viewHolder.creatSetRecycle.setAdapter(adapterSet = new BaseQuickAdapter<Integer, BaseViewHolder>(R.layout.creat_backitem, beijingBean.getBeijinglist()) {
            @Override
            protected void convert(BaseViewHolder helper, Integer item) {
                AutoUtils.autoSize(helper.itemView);
                if (helper.getLayoutPosition() == beijingposition) {
                    ((ImageView) helper.getView(R.id.creat_recycle_image)).setImageResource(beijingBean.getBeijingselectlist().get(beijingposition));
                } else {
                    ((ImageView) helper.getView(R.id.creat_recycle_image)).setImageResource(beijingBean.getBeijinglist().get(helper.getLayoutPosition()));
                }
            }
        });
        viewHolder.creatSetRecycle.setLayoutManager(new GridLayoutManager(MyBookChapterBianjiActivity.this, 5));
        adapterSet.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CreatChapterSettingManager.getInstance().saveCreatBeijingcolor(position);
                beijingposition = position;
                adapterSet.notifyDataSetChanged();
                isNeight = 0;
                CreatChapterSettingManager.getInstance().saveIsCreatNightDay(isNeight);
                viewHolder.isnighttext.setText("黑夜");
                viewHolder.isnightimage.setImageResource(R.mipmap.night);
                setBeijingAll();
            }
        });
        viewHolder.creatSetSun.setProgress(seekposition);
        viewHolder.creatSetSun.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                ScreenUtils.setScreenBrightness(progress, MyBookChapterBianjiActivity.this);
                seekposition = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                CreatChapterSettingManager.getInstance().saveCreatSeekPosition(seekposition);
            }
        });
        viewHolder.creatSetSize.setProgress(zitiposition);
        viewHolder.creatSetSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                zitiposition = progress;
                zhangjietextChapter.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12 + progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                CreatChapterSettingManager.getInstance().saveCreatZitidaxiao(zitiposition);
            }
        });
        if (isNeight == 1) {
            viewHolder.isnighttext.setText("白天");
            viewHolder.isnightimage.setImageResource(R.mipmap.creatimage_sun);
        } else {
            viewHolder.isnighttext.setText("黑夜");
            viewHolder.isnightimage.setImageResource(R.mipmap.night);
        }
        viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isNeight == 0) {
                    isNeight = 1;
                    viewHolder.isnighttext.setText("白天");
                    viewHolder.isnightimage.setImageResource(R.mipmap.creatimage_sun);
                } else {
                    isNeight = 0;
                    viewHolder.isnighttext.setText("黑夜");
                    viewHolder.isnightimage.setImageResource(R.mipmap.night);
                }
                CreatChapterSettingManager.getInstance().saveIsCreatNightDay(isNeight);
                setBeijingAll();
            }
        });
        //使其聚集
        popupWindowset.setFocusable(true);
        // 设置允许在外点击消失
        popupWindowset.setOutsideTouchable(true);
        //这个是为了点击“返回Back”也能使其消失，并且并不会影响背景
        popupWindowset.setBackgroundDrawable(new BitmapDrawable());
        // 设置SelectPicPopupWindow弹出窗体的宽
        popupWindowset.setWidth(ScreenUtils.getScreenWidth());
        popupWindowset.setHeight(ScreenUtils.getScreenHeight() * 1 / 4);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        popupWindowset.setAnimationStyle(R.style.AnimBottom);
        //popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_search_img));
        popupWindowset.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    private void setBeijingAll() {
        if (isNeight == 1) {
            creatTopChapter.setBackgroundResource(R.mipmap.top_neight);//导航栏
            setCreatChapter.setImageResource(beijingBean.getSetlist().get(0));//设置
            yijianpaibanChapter.setImageResource(beijingBean.getOnekeylist().get(0));//一键排版

            view1Chapter.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(0)));//分割线
            view2.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(0)));//分割线
            view3Chapter.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(0)));//分割线
            view4Chapter.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(0)));//分割线
            backArticleChapter.setImageResource(beijingBean.getBackimagelist().get(0));//返回
            textnumArticleChapter.setTextColor(Color.parseColor(beijingBean.getTopziticolort().get(0)));//顶部字数
            okArticleChapter.setTextColor(Color.parseColor(beijingBean.getTopziticolort().get(0)));//发布
            volume0kChapter.setTextColor(Color.parseColor("#798795"));//卷名
            zhangjienameChapter.setTextColor(Color.parseColor("#798795"));//章节名
            zhangjietextChapter.setTextColor(Color.parseColor("#798795"));//内容
            auhorSayChapterText.setTextColor(Color.parseColor("#ffffff"));//作者的话
            auhorSayChapterAddimage.setImageResource(R.mipmap.authoraddwright);
            authersayCreatChapter.setCardBackgroundColor(Color.parseColor("#3d3d3d"));
            backBgImage.setImageResource(R.mipmap.back_night);//背景
        } else {
            creatTopChapter.setBackgroundResource(beijingBean.getToplist().get(beijingposition));//导航栏
            setCreatChapter.setImageResource(beijingBean.getSetlist().get(beijingposition));//设置
            yijianpaibanChapter.setImageResource(beijingBean.getOnekeylist().get(beijingposition));//一键排版

            view1Chapter.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(beijingposition)));//分割线
            view2.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(beijingposition)));//分割线
            view3Chapter.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(beijingposition)));//分割线
            view4Chapter.setBackgroundColor(Color.parseColor(beijingBean.getFengexiancolort().get(beijingposition)));//分割线
            backArticleChapter.setImageResource(beijingBean.getBackimagelist().get(beijingposition));//返回
            textnumArticleChapter.setTextColor(Color.parseColor(beijingBean.getTopziticolort().get(beijingposition)));//顶部字数
            okArticleChapter.setTextColor(Color.parseColor(beijingBean.getTopziticolort().get(beijingposition)));//发布
            volume0kChapter.setTextColor(Color.parseColor(beijingBean.getZiticolort().get(beijingposition)));//卷名
            zhangjienameChapter.setTextColor(Color.parseColor(beijingBean.getZiticolort().get(beijingposition)));//章节名
            zhangjietextChapter.setTextColor(Color.parseColor(beijingBean.getZiticolort().get(beijingposition)));//内容
            auhorSayChapterText.setTextColor(Color.parseColor("#f77583"));//作者的话
            auhorSayChapterAddimage.setImageResource(R.mipmap.authoradd);
            authersayCreatChapter.setCardBackgroundColor(Color.parseColor("#ffffff"));
            backBgImage.setImageResource(beijingBean.getBeijingbglist().get(beijingposition));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            if (!volumId.equals(data.getStringExtra("volumId"))) {
                tomoveJuan(data.getStringExtra("volumId"), data.getStringExtra("volumName"));
            }
        }
        if (requestCode == 2 && resultCode == 2) {
            goBack();
        }
        if (requestCode == 10 && resultCode == 10) {
            authorSay = data.getStringExtra("authorsay");
        }
    }

    private void tomoveJuan(final String volumid, final String volumname) {
        OkHttpUtils.post(HttpManager.IP + "/chapterToVolume")

                .params("volumeid", volumid)
                .params("chapterid", String.valueOf(chapterInforBean.getData().getId()))
                .params("bookid", bookId)
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (noBackgLodDialog == null) {
                            noBackgLodDialog = new NoBackgLodDialog(MyBookChapterBianjiActivity.this, true);
                        }
                        noBackgLodDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        noBackgLodDialog.dismis();
                        try {
                            JSONObject js = new JSONObject(s);
                            if (js.getInt("returnCode") == 200) {
                                volumId = volumid;
                                volumName = volumname;
                                volume0kChapter.setText(volumName);
                            }
                            showToast(js.getString("msg"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    class ViewHolder {
        @BindView(R.id.creat_set_sun)
        SeekBar creatSetSun;
        @BindView(R.id.creat_set_size)
        SeekBar creatSetSize;
        @BindView(R.id.creat_set_recycle)
        RecyclerView creatSetRecycle;
        @BindView(R.id.isornight)
        LinearLayout linearLayout;
        @BindView(R.id.isnighttext)
        TextView isnighttext;
        @BindView(R.id.isnightimage)
        ImageView isnightimage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
